package com.keat.qqserver.service;

import com.keat.qqcommon.Message;
import com.keat.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Keat-Jie
 * @version 1.0
 */
public class ServerConnectClientThread extends Thread {
    private Socket socket;
    private String userId;

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {
        while (true) {
            try {
                System.out.println("服务端和客户端" + userId + " 保持通信,读取数据");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Object o = ois.readObject();

//                ConcurrentHashMap<String, ArrayList<Message>> offLineDb = new ConcurrentHashMap<>();
                if (o instanceof ArrayList) {
                    ArrayList<Message> messageArrayList = (ArrayList<Message>) o;
                    System.out.println("以下是您的离线消息：⬇");
                    for (Message message : messageArrayList) {
                        System.out.println(message.getSender() + ": " + message.getContent() + " time:");
                    }

                } else {
                    Message message=(Message)o;
                    if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)) {
                        //客户端请求在线用户列表
                        System.out.println(message.getSender() + " 要在线用户列表");
                        String onlineUser = ManageClientThreads.getOnlineUser();
                        Message message2 = new Message();
                        message2.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                        message2.setContent(onlineUser);
                        message2.setGetter(message.getSender());
                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(message2);
                    } else if (message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)) {
                        System.out.println(message.getSender() + " 退出");
                        ManageClientThreads.removeServerConnectClientThread(message.getSender());
                        socket.close();//关闭连接
                        break;
                    } else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {
                        ServerConnectClientThread serverConnectClientThread =
                                ManageClientThreads.getServerConnectClientThread(message.getGetter());
                        ObjectOutputStream oos =
                                null;
                        try {
                            oos = new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                        } catch (IOException e) {
                            System.out.println("你要发送的用户不在线");
                        }
                        oos.writeObject(message);//转发
                    } else if (message.getMesType().equals(MessageType.MESSAGE_TO_ALL_MES)) {
                        HashMap<String, ServerConnectClientThread> hm = ManageClientThreads.getHm();
                        Iterator<String> iterator = hm.keySet().iterator();
                        while (iterator.hasNext()) {
                            String onlineUserId = iterator.next().toString();
                            if (!onlineUserId.equals(message.getSender())) {//排除自己
                                ObjectOutputStream oos =
                                        new ObjectOutputStream(hm.get(onlineUserId).getSocket().getOutputStream());
                                oos.writeObject(message);
                            }
                        }

                    } else if (message.getMesType().equals(MessageType.MESSAGE_FILE_MES)) {
                        ObjectOutputStream oos =
                                new ObjectOutputStream(ManageClientThreads.getServerConnectClientThread(message.getGetter()).getSocket().getOutputStream());
                        oos.writeObject(message);

                    } else {
                        System.out.println("其他类型的message");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
